﻿using AG.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharePointOptimizer.Model;
using SharePointOptimizer.BL.DataAnalysis;

namespace SharePointOptimizer.BL.DataAnalysis.IssueTypes
{
    public class FarmTooManyWebAppsCheck :IssueTypeCheckBase
    {

        public FarmTooManyWebAppsCheck(int issueTypeId):base(issueTypeId)
        {

        }

        public override void Check()
        {
            try
            {



                // Get all Service instance that has ServiceTypeID 1
                Farm farm = dc.Farms.FirstOrDefault();


                if (farm != null)
                {

                    Service service = farm.Services.SingleOrDefault(s => s.TypeName == "Microsoft SharePoint Foundation Web Application");


                    if (service != null)
                    {
                        int webAppsCount = service.WebApplications.Count();

                        if (issueType != null)
                        {
                            if (webAppsCount > issueType.ThresholdValue)
                            {
                                Issue issue = new Issue();


                                issue.IssueType = issueType;
                                issue.TargetId = farm.FarmID;
                                issue.Description = issueType.Description.Replace("{nrWebApps}", webAppsCount.ToString());
                                issue.Description = issue.Description.Replace("{maxNrWebApps}", issueType.ThresholdValue.ToString());

                                dc.Issues.InsertOnSubmit(issue);



                                dc.SubmitChanges();
                            }
                        }


                    }

                }


                //foreach (var item in Qryinstances)
                //{
                //    if (item.NumberOfInstances > 1)
                //    {
                //        //Get Issuetype object

                //        issue = new Issue();

                //        //Get Issuetype object  And Create a new  Global issue object 
                //        IssueType issueType = dc.IssueTypes.FirstOrDefault(i => i.IssueTypeId == IssueTypeId);


                //}          





            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }

    }
       
}